#include "gl_inc.h"
#include <gl_light.h>

GLLightMgr::GLLightMgr()
{
	for(int i = 0; i < GL_MAX_LIGHT; i ++)
	{
		lightUsed[i] = -1;
	}
}

GLLightMgr::~GLLightMgr()
{
}

void GLLightMgr::Enable(Light *light)
{
	if(light->lightId == -1)
	{
		light->lightId = FindUnusedSlot();
		lightUsed[light->lightId] = 1;
	}

	if(light->lightId == -1) 
		return ; //cant enable light

	int glLightIndex = GL_LIGHT0 + light->lightId;

	glLightfv(glLightIndex, GL_AMBIENT, (float *)&light->ambient);
	glLightfv(glLightIndex, GL_DIFFUSE, (float *)&light->diffuse);
	glLightfv(glLightIndex, GL_SPECULAR,(float *)&light->specular);
	glLightfv(glLightIndex, GL_POSITION,(float *)&light->position);

	glEnable(glLightIndex);

}
void GLLightMgr::Enable(char *name)
{
	for(int i = 0; i < lightArray.size(); i ++)
	{
		if(!strcmp(name,lightArray[i]->GetName()))
		{
			Enable(lightArray[i]);
		}
	}
}

void GLLightMgr::Disable(Light *light)
{
	if(light->lightId != -1)
	{
		lightUsed[light->lightId] = -1;
		
		int glLightIndex = GL_LIGHT0 + light->lightId;
		glDisable(glLightIndex);

		light->lightId = -1;

	}
}
void GLLightMgr::Disable(char *name)
{
	for(int i = 0; i < lightArray.size(); i ++)
	{
		if(!strcmp(name,lightArray[i]->GetName()))
		{
			Disable(lightArray[i]);
		}
	}
}

int GLLightMgr::FindUnusedSlot()
{
	for(int i = 0; i < GL_MAX_LIGHT; i ++)
	{
		if(lightUsed[i] == -1)
			return i;
	}

	return -1;
}

void GLLightMgr::GlobaleAmbientLight(bool on)
{
	if(on)
	{
		glEnable (GL_LIGHTING);		//@ToDo::need to add option for these
		glShadeModel(GL_SMOOTH);
		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (float*)&ambientGlobalLight);
	}
	else
	{
		float global_ambient[] = { 0.2f, 0.2f, 0.2f, 1.0f };	//defalut value
		glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
		glDisable(GL_LIGHTING);
	}
}

